package com.yangxk.kadmin.common.util;

import java.security.MessageDigest;

/**
 * md5加密工具类
 *
 * @author yangxk
 * @date 2018/9/13 15:18
 */
public class MD5Util {

    private MD5Util(){}

    public static String md5(String src){
        try {
            StringBuffer buffer = new StringBuffer();
            char[] chars= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] data = md.digest(src!=null?src.getBytes():new byte[0]);
            for(byte b : data){
                //高4位
                buffer.append(chars[(b >> 4) & 0x0F]);
                //低4位
                buffer.append(chars[b & 0x0F]);
            }
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null ;
    }

    public static void main(String[] args) {
        String str = "123";
        System.out.println(md5(str));
    }
}
